package com.oocl.ita.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import com.oocl.ita.dao.ChecksDao;
import com.oocl.ita.po.Checks;
import com.oocl.ita.util.DBUtil;

public class ChecksDaoImpl implements ChecksDao{

	public int insertCheck(Checks checks) {
		String sql = "insert into checks(checksId,account,shopId,reason) values (checksId.nextval,?,?,?)";
		Connection connection = null;
		PreparedStatement statement = null;
		int b = 0;
		try {
			connection = DBUtil.connect();
			statement = connection.prepareStatement(sql);
			statement.setString(1, checks.getAccount());
			statement.setInt(2, checks.getShopId());
			statement.setString(3, checks.getReason());
			b = statement.executeUpdate();
		} catch (SQLException e) {
			if (connection != null) {
				try {
					connection.rollback();
					b = -1;
				} catch (SQLException e1) {
					b = -2;
				}
			}else{
				b = -3;
			}
		} finally {
			DBUtil.free(connection, statement, null);
		}
		return b;
	}

	public int deleteChecks(List<Checks> checks) {
		int m = 0;
		for(int i=0; i< checks.size(); i++){
			deleteCheck(checks.get(i).getChecksId());
			m++;
		}
		return m;
	}
	
	public int deleteCheck(int checksId) {
		String sql = "delete from Checks where checksId=?";
		Connection connection = null;
		PreparedStatement statement = null;
		int b = 0;
		try {
			connection = DBUtil.connect();
			statement = connection.prepareStatement(sql);
			statement.setInt(1, checksId);
			b = statement.executeUpdate();
		} catch (SQLException e) {
			if (connection != null) {
				try {
					connection.rollback();
					b = -1;
				} catch (SQLException e1) {
					b = -2;
				}
			}else{
				b = -3;
			}
		} finally {
			DBUtil.free(connection, statement, null);
		}
		return b;
	}

}
